package com.cjjd.c.feature.home.shoping;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cjjd.c.R;
import com.cjjd.c.base.ActionBarFragment;
import com.cjjd.c.feature.home.shoping.evaluate.EvaluateFragment;
import com.cjjd.c.feature.home.shoping.shope.GoodsFragment;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ----------------------------------
 * Created by xyd on 2017/9/22.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class ShopFragment extends ActionBarFragment {
    @Bind(R.id.tablayout)
    TabLayout tablayout;
    @Bind(R.id.viewpager)
    ViewPager viewpager;

    @Override
    public Object getTitleString() {
        return "酒店商城";
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_shoping;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }


    private Fragment goodsFragment = null;
    private Fragment evaluateFragment = null;

    @Override
    protected void initView() {
        super.initView();
        viewpager.setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {

            private String[] mTitles = new String[]{"商品", "评价"};

            @Override
            public Fragment getItem(int position) {
                if (position == 0) {
                    if (goodsFragment != null) {
                        return goodsFragment;
                    } else {
                        return new GoodsFragment();
                    }
                } else {
                    if (evaluateFragment != null) {
                        return evaluateFragment;
                    } else {
                        return new EvaluateFragment();
                    }
                }
            }

            @Override
            public int getCount() {
                return mTitles.length;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mTitles[position];
            }
        });

        tablayout.setupWithViewPager(viewpager);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
